using System;
using System.Collections.Generic;
using System.Text;
using System.Security;
using System.Drawing.Printing;
using System.Security.Permissions;
using System.Runtime.InteropServices;

namespace qPrintComponent
{
    public static class qStatic
    {
        private static TimeSpan span1 = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now);
        internal static int hourdiv = span1.Hours * 60;
        
        [SecurityCritical, SecuritySafeCriticalAttribute, PrintingPermission(SecurityAction.Demand, Level = PrintingPermissionLevel.DefaultPrinting)]
        public static string GetDefaultPrinter()
        {
            string defaultprinter;
            int num1;
            GetDefaultPrinterW(null, out num1);
            defaultprinter = new string(' ', num1);
            if (GetDefaultPrinterW(defaultprinter, out num1))
                defaultprinter = defaultprinter.TrimEnd('\0');
            return defaultprinter;
        }
        
        [DllImport("winspool.drv", EntryPoint = "OpenPrinterW", CharSet = CharSet.Unicode, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool OpenPrinter(string PrinterName, out IntPtr PrinterHandle, ref qPrinterDefaults PrinterDefaults);

        [DllImport("winspool.drv", EntryPoint = "OpenPrinterW", CharSet = CharSet.Unicode, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool OpenPrinter(string PrinterName, out IntPtr PrinterHandle, int PrinterDefaults);
        
        [DllImport("winspool.Drv", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool ClosePrinter(IntPtr hPrinter);

        [DllImport("winspool.drv", EntryPoint = "GetDefaultPrinterW", CharSet = CharSet.Unicode)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool GetDefaultPrinterW(string PrinterName, out int dwNeeded);

        [DllImport("winspool.drv", EntryPoint = "SetDefaultPrinter", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool SetDefaultprinter(string Printername);

        [DllImport("winspool.drv", EntryPoint = "GetPrinterW", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool GetPrinter(IntPtr hPrinter, int Level, IntPtr pPrinter, int cbBuf, out int dwNeeded);

        [DllImport("winspool.drv", EntryPoint = "SetPrinterW", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool SetPrinter(IntPtr hPrinter, int Level, IntPtr pPrinter, [In, MarshalAs(UnmanagedType.U4)] PrinterControl Command);

        [DllImport("winspool.drv", EntryPoint = "GetPrinterDriverW", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool GetPrinterDriver(IntPtr PrinterHandle, IntPtr pEnvironment, int Level, IntPtr pPrinter, int cbBuf, out int dwNeeded);

        [DllImport("winspool.drv", EntryPoint = "EnumPrinterDriversW", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool EnumPrinterDrivers([In] string ServerName, [In] string Environmentname, [In] int Level, [Out] IntPtr pdrivers, [In] int cbBuf, out int pcbNeeded, out int pcbReturned);

        [DllImport("winspool.drv", EntryPoint = "DeviceCapabilitiesW", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
        internal static extern int DeviceCapabilitiesW(string sDevice, string Port, int fwCapability, IntPtr Output, IntPtr device);
    }
}
